package biz

import (
	pb "blog/api/blogs/v1"
	"blog/api/global"
	"blog/internal/constx"
	"context"
	"errors"
)

type BlogGraphQuery struct {
	*global.PageInfo
	Id        string
	Title     string
	AccountId int64 //当前登陆人ID
	BlogId    int64
	ProjectId int64
}
type BlogGraph struct {
	Id           string
	AccountId    int64
	Title        string
	Description  string
	IsHidden     int64
	Content      string
	BrowseNumber int64
	BlogId       int64
	BlogTitle    string
	ProjectId    int64
	ProjectTitle string
	CreatedAt    int64
	UpdatedAt    int64
}

func (s *BlogsUseCase) BindGraph(ctx context.Context, req *pb.BindGraphReq) (*global.Empty, error) {
	return &global.Empty{}, s.repo.BindGraph(ctx, req.Id, req.GraphId)
}

func (s *BlogsUseCase) ListGraph(ctx context.Context, req *pb.ListGraphRequest) (*pb.ListGraphReply, error) {
	user := constx.DefaultUser.Default(ctx)
	total, list, err := s.repo.ListGraph(ctx, &BlogGraphQuery{
		PageInfo:  req.Page,
		Title:     req.Title,
		AccountId: user.Id,
		BlogId:    req.BlogId,
		ProjectId: req.ProjectId,
	})
	if err != nil {
		return nil, err
	}
	res := &pb.ListGraphReply{
		Total: total,
		List:  make([]*pb.GraphItem, 0),
	}
	for _, graph := range list {
		res.List = append(res.List, &pb.GraphItem{
			Id:           graph.Id,
			Title:        graph.Title,
			Description:  graph.Description,
			BrowseNumber: graph.BrowseNumber,
			//Content:      graph.Content,
			BlogId:       graph.BlogId,
			BlogTitle:    graph.BlogTitle,
			ProjectId:    graph.ProjectId,
			IsHidden:     graph.IsHidden,
			ProjectTitle: graph.ProjectTitle,
			CreatedAt:    graph.CreatedAt,
			UpdatedAt:    graph.UpdatedAt,
			AccountId:    graph.AccountId,
		})
	}
	return res, nil
}

func (s *BlogsUseCase) SaveGraph(ctx context.Context, req *pb.GraphItem) (*global.Empty, error) {
	if req.Content == "" {
		err := errors.New("数据不能为空")
		return nil, err
	}
	if req.Title == "" {
		err := errors.New("标题不能为空")
		return nil, err
	}
	u, err := constx.DefaultUser.User(ctx)
	if err != nil {
		return nil, err
	}
	data := &BlogGraph{
		Id:          req.Id,
		Title:       req.Title,
		Description: req.Description,
		IsHidden:    req.IsHidden,
		Content:     req.Content,
		AccountId:   u.Id,
		BlogId:      req.BlogId,
		ProjectId:   req.ProjectId,
	}
	return &global.Empty{}, s.repo.SaveGraph(ctx, data)
}

func (s *BlogsUseCase) CheckGraph(ctx context.Context, req *global.IDStr) (*pb.GraphItem, error) {
	user := constx.DefaultUser.Default(ctx)
	data, err := s.repo.CheckGraph(ctx, &BlogGraphQuery{
		Id:        req.Id,
		AccountId: user.Id,
	})
	if err != nil {
		return nil, err
	}
	return &pb.GraphItem{
		Id:           data.Id,
		Title:        data.Title,
		Description:  data.Description,
		BrowseNumber: data.BrowseNumber,
		Content:      data.Content,
		IsHidden:     data.IsHidden,
		BlogId:       data.BlogId,
		BlogTitle:    data.BlogTitle,
		ProjectId:    data.ProjectId,
		ProjectTitle: data.ProjectTitle,
		CreatedAt:    data.CreatedAt,
		UpdatedAt:    data.UpdatedAt,
		AccountId:    data.AccountId,
	}, nil
}

func (s *BlogsUseCase) DelGraph(ctx context.Context, req *global.IDStr) (*global.Empty, error) {
	user, err := constx.DefaultUser.User(ctx)
	if err != nil {
		return nil, err
	}
	err = s.repo.DelGraph(ctx, &BlogGraphQuery{
		Id:        req.Id,
		AccountId: user.Id,
	})
	return &global.Empty{}, err
}
